//
//  MainViewController.m
//  atXiaoGeBClient
//
//  Created by Linyu Niu on 20/04/2018.
//  Copyright © 2018 Linyu Niu. All rights reserved.
//

#import "MainViewController.h"

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UISegmentedControl * segmentedControl;
    UITableView * mainTableView;
    NSArray * dataList;
    BOOL authSuccess;
    BOOL isOnDuty;//是否在上班
}
@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusChange:) name:@"kCourierStatusChangeNotification" object:nil];
    __weak MainViewController * weakSelf = self;

    UIView * navView = [[CTMediator sharedInstance] navgationHeadViewInTargetView:self.view params:[NSDictionary dictionaryWithObjectsAndKeys:@"home_personal",@"leftBtnImage",@"alarm_icon",@"rightBtnImage", nil] leftAction:^(UIButton *sender){
        UIViewController * controller = [[CTMediator sharedInstance] personalCenterController];
        [weakSelf.navigationController pushViewController:controller animated:YES];
    } rightAction:^(UIButton *sender){
        UIViewController * controller = [[CTMediator sharedInstance] messageController];
        [weakSelf.navigationController pushViewController:controller animated:YES];
    }];
    
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"待抢订单",@"我的订单", nil]];
    segmentedControl.selectedSegmentIndex = 0;
    [segmentedControl addTarget:self action:@selector(onClickSegment:) forControlEvents:UIControlEventValueChanged];
    [navView addSubview:segmentedControl];
    [segmentedControl setTintColor:[UIColor whiteColor]];
    UIFont *font = [UIFont systemFontOfSize:16.0f];
    NSDictionary *attributes = [NSDictionary dictionaryWithObject:font
                                                           forKey:NSFontAttributeName];
    [segmentedControl setTitleTextAttributes:attributes
                                    forState:UIControlStateNormal];
    
    segmentedControl.frame = CGRectMake((navView.frame.size.width - 200) / 2, (navView.frame.size.height - 32) / 2 + [UIApplication sharedApplication].statusBarFrame.size.height / 2, 200, 32);
    [navView addSubview:segmentedControl];
    
    UIView * bottomView = [self addBottomView];
    mainTableView = [self addTableViewInView:self.view frame:CGRectMake(0, navView.frame.origin.y + navView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - navView.frame.origin.y - navView.frame.size.height - bottomView.frame.size.height) delegateAndDataSource:self];
}


#pragma mark-Notification
-(void)statusChange:(NSNotification *)notify{
    NSDictionary * dic = [notify object];
    authSuccess = [self isSuccess:dic];
    [mainTableView reloadData];
}
-(UIView *)addBottomView{
    UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height - 45, self.view.frame.size.width, 45)];
    [self.view addSubview:bgView];
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, bgView.frame.size.width / 2, bgView.frame.size.height);
    leftButton.backgroundColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftButton setTitle:@"网点派件" forState:UIControlStateNormal];
    [leftButton setImageEdgeInsets:UIEdgeInsetsMake(0, leftButton.frame.size.width / 2  - 100, 0, 0)];
    [leftButton setImage:[UIImage imageNamed:@"wang_diang"] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:leftButton];
    
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(bgView.frame.size.width / 2, 0, bgView.frame.size.width / 2, bgView.frame.size.height);
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitle:@"上班抢单" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(onDutyDeal:) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    rightButton.backgroundColor = UIColorFromRGB(0x20C67A);
    [bgView addSubview:rightButton];
    
    return bgView;
}

-(void)onDutyDeal:(UIButton *)sender{
    __weak MainViewController * weakSelf = self;
    [self startLoadingView:self.view];
    if (!isOnDuty) {
        [sender setTitle:@"下班" forState:UIControlStateNormal];
        [[CTMediator sharedInstance] onDutyDeal:^(NSDictionary * dic){
            [weakSelf stopLoadingView:weakSelf.view needShowMsg:YES result:dic];
        }];
    }else{
        [sender setTitle:@"上班抢单" forState:UIControlStateNormal];
        [[CTMediator sharedInstance] downDutyDeal:^(NSDictionary * dic){
            [weakSelf stopLoadingView:weakSelf.view needShowMsg:YES result:dic];
        }];
    }
    isOnDuty = !isOnDuty;
    [mainTableView reloadData];
}

-(void)statusAction{
    //个人信息
    /*
    UIViewController * controller = [[CTMediator sharedInstance] personalController];
    
    */
    
    //没有开通权限
    /*
    UIViewController * controller = [[CTMediator sharedInstance] noAuthController];
    */
    
    //我的钱包
    
//    UIViewController * controller = [[CTMediator sharedInstance] myWalletController];
    //绑定银行卡
//    UIViewController * controller = [[CTMediator sharedInstance] bindCardController];
    //提现
//    UIViewController * controller = [[CTMediator sharedInstance]checkController];
    
    
    //派件
//    UIViewController * controller = [[CTMediator sharedInstance] assignController];
    //个人中心
    UIViewController * controller = [[CTMediator sharedInstance] personalCenterController];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)onClickSegment:(UISegmentedControl *)sender{
    
    if (sender.tag == 0) {
        [[CTMediator sharedInstance] rushOrderListWithLocation:@"" block:^(NSDictionary * dic){
            
        }];
    }else{
        //我的订单
        [[CTMediator sharedInstance] myOrderList:^(NSDictionary * dic){
            
        }];
    }
    [mainTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (!authSuccess) {
        return 1;
    }
    
    if(segmentedControl.selectedSegmentIndex == 0){
        if (isOnDuty) {
            return [dataList count];
        }else{
            return 1;
        }
    }
#if kDevelopment
    return 10;
#else
    
    
    return [dataList count];
#endif
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (authSuccess) {
        if (segmentedControl.selectedSegmentIndex == 0) {
            if (isOnDuty) {
                return [[CTMediator sharedInstance] createInReceivingCell:[dataList objectAtIndex:[indexPath row]] index:indexPath];
            }else{
                ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
                cell.backgroundColor = UIColorFromRGB(0xE9E9E9);
                cell.contentView.backgroundColor = UIColorFromRGB(0xE9E9E9);
                
                cell.cellHeight = tableView.frame.size.height;
                UIImageView * imgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"order_offline_tip"]];
                imgView.frame = CGRectMake(0, 100, tableView.frame.size.width, 100);
                imgView.contentMode = UIViewContentModeScaleAspectFit;
                [cell addSubview:imgView];
                
                
                UILabel *label = [[UILabel alloc] init];
                label.frame = CGRectMake(0,imgView.frame.size.height + imgView.frame.origin.y + 10,tableView.frame.size.width,30);
                label.text = @"下班中，现在不能收到订单哦~";
                label.font = [UIFont systemFontOfSize:15];
                label.textColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1];
                [cell addSubview:label];
                return cell;
            }
        }else if(segmentedControl.selectedSegmentIndex == 1){
            
            return [[CTMediator sharedInstance] createMyOrderCell:[dataList objectAtIndex:[indexPath row]] index:indexPath];
            
        }
    }
    
    ElementTableViewCell * cell = [[ElementTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    UIImageView * imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 100, tableView.frame.size.width, 50)];
    imgView.contentMode = UIViewContentModeScaleAspectFit;
    imgView.image = [UIImage imageNamed:@"account_verify"];//account_fail;//account_unComplete;//warning_icon
    [cell addSubview:imgView];
    
    UILabel * label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, imgView.frame.origin.y + imgView.frame.size.height + 15, tableView.frame.size.width,25);
    label.text = @"身份审核中";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:22];
    label.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [cell addSubview:label];
    
    UILabel * desLabel = [[UILabel alloc] init];
    desLabel.frame = CGRectMake(0, label.frame.origin.y + label.frame.size.height + 5, tableView.frame.size.width, 5.7);
    desLabel.numberOfLines = 0;
    desLabel.textAlignment = NSTextAlignmentCenter;
    desLabel.text = @"您的账号正在审核中，通过后可接单";
    desLabel.font = [UIFont systemFontOfSize:14];
    desLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    [desLabel enlargeHeightToKeepFontSize];
    [cell addSubview:desLabel];
    
    UIButton * button = [[CTMediator sharedInstance] commonOrangeButton:CGRectMake(40, desLabel.frame.origin.y + desLabel.frame.size.height + 20, tableView.frame.size.width - 80, 40) title:@"联系小哥加速审核速度" delegate:self action:nil];
    [button addTarget:self action:@selector(statusAction) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:button];
    cell.cellHeight = button.frame.size.height + button.frame.origin.y + 50;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ElementTableViewCell * cell = (ElementTableViewCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (authSuccess) {
        if(segmentedControl.selectedSegmentIndex == 0){
            UIViewController * controller = [[CTMediator sharedInstance] confirmFetchController];
            [self.navigationController pushViewController:controller animated:YES];
        }else if (segmentedControl.selectedSegmentIndex == 1) {
            UIViewController * controller = [[CTMediator sharedInstance] waitForFetchController];
            [self.navigationController pushViewController:controller animated:YES];
        }
    }
}


-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
